﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Common.Library;
using Windows.Common.Data;
using OnlineVideos.Entities;
using System.Threading.Tasks;

namespace OnlineMovies.Views
{
    public partial class Description : PhoneApplicationPage
    {
        //AddShow addshow = new AddShow();
        public Description()
        {
            InitializeComponent();
            Loaded += Description_Loaded;
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("Description Page");
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In Description.cs file.", ex);
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In Description.cs file.", ex);
            }
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            Constants.DownloadTimer.Start();
        }
        void Description_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogEvent("Description Page", true);
                txtlimit.Visibility = Visibility.Collapsed;
                if (Constants.editdescription == true)
                {
                    int showid = AppSettings.ShowUniqueID;
                    List<ShowList> showlist = Task.Run(async () => await Constants.connection.Table<ShowList>().ToListAsync()).Result;
                    string des = showlist.Where(i => i.ShowID == showid).FirstOrDefault().Description;
                    txtdes.Text = des;

                }
                else
                {
                    txtdes.Text = Constants.Description.ToString();
                }
                txtlength.Text = Convert.ToString(txtdes.Text.Length) + "/" + Convert.ToString(4000);
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in Description_Loaded Method In Description.cs file.", ex);
            }
        }

        private void imgTitle_MouseEnter(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {

        }

        private async void btnsave_Click_1(object sender, EventArgs e)
        {
            try
            {
                List<ShowList> showlist = Task.Run(async () => await Constants.connection.Table<ShowList>().ToListAsync()).Result;
                if (txtdes.Text.Length > 4000)
                {

                    txtlimit.Visibility = Visibility.Visible;
                }
                else
                {
                    txtlimit.Visibility = Visibility.Collapsed;
                    if (Constants.editdescription == true)
                    {
                        int showid = AppSettings.ShowUniqueID;
                        Constants.editdescription = false;
                        ShowList s = showlist.Where(i => i.ShowID == showid).FirstOrDefault();
                        s.Description = txtdes.Text;
                        await Constants.connection.UpdateAsync(s);
                        Constants.DownloadTimer.Start();
                    }
                    else
                    {
                        Constants.Description.Clear();
                        Constants.Description.Append(txtdes.Text);
                    }

                    NavigationService.GoBack();
                }
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in btnsave_Click_1 Method In Description.cs file.", ex);
            }
        }

        private void txtdes_TextChanged_1(object sender, TextChangedEventArgs e)
        {
            txtlimit.Visibility = Visibility.Collapsed;
            txtlength.Text = Convert.ToString(txtdes.Text.Length) +"/" +Convert.ToString(4000);
        }
    }
}